package com.chinatelecom.ifree.business.dao.vote.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.chinatelecom.ifree.business.dao.base.DbcpPool;
import com.chinatelecom.ifree.business.dao.order.impl.BaseDaoSupport;
import com.chinatelecom.ifree.business.dao.vote.VoteDao;
import com.chinatelecom.ifree.business.domain.vote.VoteDetail;
@Repository
public class VoteDaoImpl extends BaseDaoSupport<VoteDetail> implements VoteDao {
	
	private static transient Logger logger = LoggerFactory.getLogger(VoteDaoImpl.class) ;

	@Override
	public Map<Long, Integer> getVoteCountByVoteId(long voteid) throws Exception {
		sql = "SELECT vote_item_id,count(*) FROM vote_detail WHERE vote_id=? group by vote_item_id";
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try {
			Map<Long, Integer> map = new HashMap<Long, Integer>() ;
			conn = DbcpPool.getInstance().getConn();
			statement = conn.prepareStatement(sql);
			statement.setLong(1, voteid);
			rs = statement.executeQuery();
			while (rs.next()) {
				map.put(rs.getLong(1),rs.getInt(2)) ;
			}
			return map ;
		} catch (Exception e) {
			logger.error("getVoteCountByVoteId", e);
			throw e;
		} finally {
			DbcpPool.getInstance().closeConnection(rs, statement, conn);
		}
	}

	@Override
	public void saveVoteDetail(VoteDetail voteDetail) throws Exception {
		sql = "INSERT INTO vote_detail(third_id,vote_id,vote_item_id,create_time,remark) VALUES(?,?,?,?,?)";
		
		super.saveEntity(voteDetail);
	}
	
	@Override
	public void saveVoteDetailList(VoteDetail[] voteDetailArr) throws Exception {
		sql = "INSERT INTO vote_detail(third_id,vote_id,vote_item_id,create_time,remark,cookie) VALUES(?,?,?,?,?,?)";
		
		super.saveBatchEntity(voteDetailArr);
	}
	
	@Override
	public void executeSql(PreparedStatement statement, VoteDetail voteDetail) throws SQLException {
		statement.setLong(1, voteDetail.getThirdId());
		statement.setLong(2, voteDetail.getVoteId());
		statement.setLong(3, voteDetail.getVoteItemId());
		statement.setDate(4, new Date(voteDetail.getCreateTime().getTime()));
		statement.setString(5, voteDetail.getRemark());
	}

	@Override
	public VoteDetail executeSingleResultSet(ResultSet rs) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void executeBatchSql(PreparedStatement statement, VoteDetail[] t)
			throws Exception {
		for (int i = 0; i < t.length; i++) {
			statement.setLong(1, t[i].getThirdId());
			statement.setLong(2, t[i].getVoteId());
			statement.setLong(3, t[i].getVoteItemId());
			statement.setDate(4, new Date(t[i].getCreateTime().getTime()));
			statement.setString(5, t[i].getRemark());
			statement.setString(6, t[i].getCookie());
			
			statement.addBatch();
		}
		
	}

	@Override
	public int getVoteCountByVoteItemId(long voteitemid) throws Exception {
		//sql = "SELECT count(*) FROM vote_detail WHERE vote_id=? and vote_item_id=?";
		sql = "SELECT count(*) FROM vote_detail WHERE vote_item_id=?";
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try {
			conn = DbcpPool.getInstance().getConn();
			statement = conn.prepareStatement(sql);
			statement.setLong(1, voteitemid);
			rs = statement.executeQuery();
			while (rs.next()) {
				return rs.getInt(1) ;
			}
		} catch (Exception e) {
			logger.error("getVoteCountByVoteId", e);
			throw e;
		} finally {
			DbcpPool.getInstance().closeConnection(rs, statement, conn);
		}
		return 0;
	}

	@Override
	public Map<Long, Integer> getVoteCountByVoteIdAndCookie(long voteid,
			String cookie) throws Exception {
		sql = "SELECT vote_item_id,count(*) FROM vote_detail WHERE vote_id=? and vote_item_id in "
				+ "(select vote_item_id from vote_detail where cookie=? and vote_id=?) group by vote_item_id";
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try {
			Map<Long, Integer> map = new HashMap<Long, Integer>() ;
			conn = DbcpPool.getInstance().getConn();
			statement = conn.prepareStatement(sql);
			statement.setLong(1, voteid);
			statement.setString(2, cookie);
			statement.setLong(3, voteid);
			rs = statement.executeQuery();
			while (rs.next()) {
				map.put(rs.getLong(1),rs.getInt(2)) ;
			}
			return map ;
		} catch (Exception e) {
			logger.error("getVoteCountByVoteId", e);
			throw e;
		} finally {
			DbcpPool.getInstance().closeConnection(rs, statement, conn);
		}
	}
}
